/**
 * 固定表头 js库 
 * 提示: 
 * 	1. 如果页面发生js错误，请检查页面编码格式是否使UTF-8 
 * 		非UTF-8页面引用本js脚本时，可能发生浏览器无法识别部分js内容
 */

/**
 * 固定表头
 * @param oTable  被操作的表
 * @param fixedRowArray 被固定的行索引数组
 * @param fixedCellArray 被固定的列
 * @param fixedFootArray 被固定的末尾
 */
function FixedTable(oTable, fixedRowArray, fixedCellArray, fixedFootArray) {
	this.fixedRows = new Array();
	this.fixedCells = new Array();
	this.fixedFoots = new Array();
	var theTable = this.getElement(oTable, "TABLE"); // 获取页面中的Table
	if (theTable == null) throw {message: "[" + oTable + "]不是有效的Table标识 " };

	if (fixedRowArray) {
		if (fixedRowArray instanceof Array) {
			for (var i = 0; i < fixedRowArray.length; i++) {
				this.fixedRow(theTable, fixedRowArray[i]);
			}
		} else {
			this.fixedRow(theTable, fixedRowArray);
		}
	}

	if (fixedFootArray) {
		if (fixedFootArray instanceof Array) {
			for (var i = 0; i < fixedFootArray.length; i++) {
				this.fixedFoot(theTable, fixedFootArray[i]);
			}
		} else {
			this.fixedFoot(theTable, fixedFootArray);
		}
	}

	if (fixedCellArray) {
		if (fixedCellArray instanceof Array) {
			for (var i = 0; i < fixedCellArray.length; i++) {
				this.fixedCell(theTable, fixedCellArray[i]);
			}
		} else {
			this.fixedCell(theTable, fixedCellArray);
		}
	}
	return this;
}

/**
 * 获取Document里面的某个元素
 * @param oKey 页面元素标识
 * @param tagName 匹配的TagName 如果tagName不存在那么当作任意对象
 */
FixedTable.prototype.getElement = function(oKey, tagName) {
	if (oKey == undefined || oKey == null) return null;
	if (tagName === undefined || tagName === null) return typeof(oKey) == "string" ? document.getElementById(oKey) : oKey;
	if (oKey.tagName == tagName) return oKey;
	if (typeof(oKey) == "string") {
		var vObj = document.getElementById(oKey);
		if (vObj && vObj.tagName == tagName) return vObj;
	}
	return null;
};

FixedTable.prototype.fixedRow = function(oTable, rowIndex) {
	if (rowIndex >= 0 && oTable.rows.length > rowIndex) {
		var row = oTable.rows[rowIndex];
		row.className = "FixedTitleRow";
		this.fixedRows[rowIndex] = "1";
	} else {
		throw {message: "固定的行索引[" + rowIndex + "]不是有效的值 "};
	}
};

FixedTable.prototype.fixedFoot = function(oTable, rowIndex) {
	rowIndex = -rowIndex;
	if (rowIndex >= 0 && oTable.rows.length > rowIndex) {
		var row = oTable.rows[oTable.rows.length - 1 - rowIndex];
		row.className = "FixedFootRow";
		this.fixedFoots[oTable.rows.length - 1 - rowIndex] = "1";
	} else {
		throw {message: "固定的尾行索引[" + (-rowIndex) + "]不是有效的值 "};
	}
};

FixedTable.prototype.fixedCell = function(oTable, cellIndex) {
	var rowSpanArray = new Array();
	for (var i = 0; i < oTable.rows.length; i++) {
		var row = oTable.rows[i];
		var detCellIndex = 0;
		for (var j = 0; j < cellIndex; j++) {
			if (rowSpanArray[j] > 0) {
				rowSpanArray[j] = rowSpanArray[j] - 1;
				detCellIndex--;
			} else {
				var cell = row.cells[j + detCellIndex];
				if (cell.colSpan > 1) {
					detCellIndex -= cell.colSpan - 1;
					if (cell.rowSpan > 1) {
						for (var k = 0; k < cell.colSpan; k++) {
							rowSpanArray[j++] = cell.rowSpan - 1;
						}
					} else {
						j += cell.colSpan - 1;
					}
				} else if (cell.rowSpan > 1) {
					rowSpanArray[j] = cell.rowSpan - 1;
				}
			}
		}
		if (cellIndex + detCellIndex >= 0
				&& row.cells.length > cellIndex + detCellIndex) {
			if (rowSpanArray[cellIndex] > 0) {
				rowSpanArray[cellIndex] = rowSpanArray[cellIndex] - 1;
			} else {
				var cell = row.cells[cellIndex + detCellIndex];
				if (this.fixedRows[i] == "1" || this.fixedFoots[i] == "1") {
					cell.className = "FixedTitleColumn";
				} else {
					cell.className = "FixedDataColumn";
				}
				this.fixedCells[cellIndex] = "1";
				if (cell.rowSpan > 1) {
					rowSpanArray[cellIndex] = cell.rowSpan - 1;
				}
			}
		}
	}
};